//
//  LGSportsEvaFillinController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/10.
//  填写评估数据

#import "LGSportsEvaInputController.h"
#import "LGRecoverySectionView.h"
#import "LGSportsEvaInputCell.h"
#import "LGSportsEvaSelectCell.h"
#import "DatePickerTwoView.h"
@interface LGSportsEvaInputController ()<UITableViewDelegate,UITableViewDataSource,DatePickerViewDelegate,LGSportsEvaInputDelegate,LGSportsEvaSelectDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *dateButton;
/**   */
@property (nonatomic , strong)UIButton *saveButton;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)NSMutableDictionary *resultDic;
/**   */
@property (nonatomic , copy)NSString *dateStr;

@end

@implementation LGSportsEvaInputController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dateStr = @"";
    self.title = @"填写评估数据";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.saveButton];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/sport.estimate/question" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            NSArray *tempArray = returnData;
            NSMutableArray *mutArray = [NSMutableArray array];
            for (NSInteger i=0; i<tempArray.count; i++) {
                NSArray *contentArray = tempArray[i][@"content"];
                [mutArray addObjectsFromArray:contentArray];
                for (NSInteger j=0; j<contentArray.count; j++) {
                    NSArray *questionArray = contentArray[j][@"questions"];
                    for (NSInteger m=0; m<questionArray.count; m++) {
                        NSArray *itemArray = questionArray[m][@"items"];
                        for (NSDictionary *tempDic in itemArray) {
                            [self.resultDic setValue:@"" forKey:LGNSString(tempDic[@"name"])];
                        }
                    }
                }
            }
            self.dataArray = [NSArray arrayWithArray:mutArray];
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
            
    }];
}

//选择日期
-(void)selectDateAction{
    [self.view endEditing:YES];
    DatePickerTwoView *pickerView = [[DatePickerTwoView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.delegate = self;
    pickerView.title = @"评估日期";
    [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
}

-(void)sendSelectDate:(NSString *)date index:(NSInteger)index{
    self.dateStr = date;
    [self.dateButton setTitle:date forState:UIControlStateNormal];
    [self.dateButton setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:3];
}

//输入文字
-(void)inputContent:(NSString *)content key:(NSString *)key{
    if(content.length>0){
        [self.resultDic setValue:content forKey:key];
    }else{
        [self.resultDic removeObjectForKey:key];
    }
}

//选择项
-(void)selectContent:(NSString *)title  select:(BOOL)selected key:(NSString *)key{
    [self.view endEditing:YES];
    if(selected == YES){
        [self.resultDic setValue:title forKey:key];
    }else{
        [self.resultDic removeObjectForKey:key];
    }
}

//保存
-(void)saveDataAction{
    if(self.dateStr.length == 0){
        [TooltipView showMessage:@"请选择评估日期" offset:0];
        return;
    }
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:self.resultDic];
    [tempDic setValue:self.patientid forKey:@"patient_id"];
    [tempDic setValue:self.dateStr forKey:@"estimate_date"];
    [RequestUtil withPOST:@"/doctor/sport.estimate/save" parameters:tempDic withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            if(self.saveDataSuccess){
                self.saveDataSuccess();
            }
            [TooltipView showMessage:@"保存成功" offset:0];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:@"保存失败，请重试" offset:0];
    }];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count+1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section > 0){
        NSDictionary *tempDic = self.dataArray[section-1];
        return [tempDic[@"questions"] count];
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.section-1][@"questions"][indexPath.row];
    NSString *type = LGNSString(tempDic[@"type"]);
    if([type isEqualToString:@"text"]){
        LGSportsEvaInputCell *cell = [tableView dequeueReusableCellWithIdentifier:@"inputCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.resultDic = self.resultDic;
        cell.indexPath = indexPath;
        cell.dataDic = tempDic;
        cell.delegate = self;
        return cell;
    }else{
        LGSportsEvaSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.resultDic = self.resultDic;
        cell.dataDic = tempDic;
        cell.delegate = self;
        return cell;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return (section == 0)?viewPix(50):viewPix(35);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    CGFloat height = (section == 0)?viewPix(50):viewPix(35);
    LGRecoverySectionView *headView = [[LGRecoverySectionView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, height)];
    if(section == 0){
        headView.title = @"评估日期";
        [headView addSubview:self.dateButton];
        
    }else{
        NSDictionary *tempDic = self.dataArray[section-1];
        headView.title = LGNSString(tempDic[@"group_name"]);
    }
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(5);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(5))];
    footView.backgroundColor = [UIColor whiteColor];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(3), Screen_W-viewPix(32), 0.5)];
    lineView.backgroundColor = [UIColor colorWithString:@"#E0E0E0"];
    [footView addSubview:lineView];
    return footView;
}

#pragma mark
#pragma mark ====> 创建控件
-(NSMutableDictionary *)resultDic{
    if(!_resultDic){
        _resultDic = [NSMutableDictionary dictionary];
    }
    return _resultDic;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(84)-bottomSafeBarHeight/4.0) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGSportsEvaInputCell class] forCellReuseIdentifier:@"inputCell"];
        [_tableView registerClass:[LGSportsEvaSelectCell class] forCellReuseIdentifier:@"selectCell"];
        _tableView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(10))];
    }
    return _tableView;
}

-(UIButton *)dateButton{
    if(!_dateButton){
        _dateButton = [UIButton buttonWithTitle:@"请选择" titleFont:LGFont(14) textColor:@"#666666" imageName:@"grayTip12"];
        _dateButton.frame = CGRectMake(Screen_W-viewPix(166), 0, viewPix(150), viewPix(50));
        _dateButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_dateButton setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:3];
        [_dateButton addTarget:self action:@selector(selectDateAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _dateButton;
}

-(UIButton *)saveButton{
    if(!_saveButton){
        _saveButton = [UIButton buttonWithTitle:@"保存" topMargin:kMaxY(self.tableView.frame)+viewPix(12)];
        [_saveButton addTarget:self action:@selector(saveDataAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveButton;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
